package com.falling.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.falling.reggie.common.R;
import com.falling.reggie.entity.Employee;
import com.falling.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Resource
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request){
        // 请求体密码加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        // 查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee one = employeeService.getOne(queryWrapper);

        // 判断是否可以登录
        if (one == null || !password.equals(one.getPassword())) return R.error("login fail");
        if (one.getStatus() == 0) return R.error("account unused");

        // 存入session并返回给浏览器
        request.getSession().setAttribute("employee", one.getId());

        return R.success(one);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("exit success");
    }

    @PostMapping
    public R<String> save(@RequestBody Employee employee){
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());

//        long id = (long)request.getSession().getAttribute("employee");

//        employee.setCreateUser(id);
//        employee.setUpdateUser(id);

        if(employeeService.save(employee)) return R.success("save success...");
        else return R.error("save fail...");

    }

    @GetMapping("/page")
    public R<Page<Employee>> page(int page, int pageSize, String name){

        Page<Employee> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        queryWrapper.orderByDesc(Employee::getUpdateTime);

        employeeService.page(pageInfo, queryWrapper);

        if (pageInfo.getTotal() != 0) return R.success(pageInfo);
        else return R.error("no field");
    }

    @PutMapping
    public R<String> updateStatus(@RequestBody Employee employee){
//        long empId = (long)request.getSession().getAttribute("employee");
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(empId);

        if (employeeService.updateById(employee)) return R.success("update status success...");
        else  return R.error("update status fail");
    }

    @GetMapping("/{id}")
    public R<Employee> revealInfo(@PathVariable long id){
        Employee emp = employeeService.getById(id);
        if (emp != null) return R.success(emp);
        else return R.error("query and reveal fail...");
    }

}
